package com.example.myfinalhomework;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class xijies extends AppCompatActivity {
    private static final String TAG = "detail";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xijie);
        String nam=getIntent().getStringExtra("name");
        TextView num=findViewById(R.id.phone);
        Button call_phone=findViewById(R.id.dephone);
        Log.i(TAG,"1");

        call_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            //这个地方用到了非本人写的代码，为获取权限，实现电话短信效果！！！！！


            public void onClick(View view) {
                int a= ContextCompat.checkSelfPermission(xijies.this, android.Manifest.permission.CALL_PHONE);
                if(a== PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(xijies.this, "有权限", Toast.LENGTH_SHORT).show();
                    Log.d(TAG,"有权限");
                    Log.i(TAG,"2");
                    makePhoneCall( num.getText().toString().trim());
                }else{
                    ActivityCompat.requestPermissions(xijies.this,new String[]{Manifest.permission.CALL_PHONE},1);
                    Log.d(TAG,"无权限");
                    Toast.makeText(xijies.this, "无权限", Toast.LENGTH_SHORT).show();
                }
            }
        });


        Button dmessage=findViewById(R.id.demessage);
        dmessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG,"3");
                Intent intent = new Intent(Intent.ACTION_SENDTO);
                Uri uri = Uri.parse("smsto:" + Uri.encode(num.getText().toString().trim()));
                intent.setData(uri);
                startActivity(intent);
            }
        });
        Button de_back=findViewById(R.id.deback);
        de_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG,"4");
                Intent intent=new Intent(xijies.this, Main.class);
                startActivity(intent);

            }
        });
        Button  de_del=findViewById(R.id. dedel);
        de_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG,"5");
                TXmanager.delete(nam);
                Toast.makeText(xijies.this, "删除成功", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(xijies.this, Main.class);
                startActivity(intent);
            }
        });
        Button  de_up=findViewById(R.id.deup);
        de_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG,"6");
                Intent intent=new Intent(xijies.this, Change.class);
                intent.putExtra("name",nam);
                startActivity(intent);
            }
        });

        userrelated tx = TXmanager.getByName(nam);
        TextView name=findViewById(R.id.dename);
        name.setText(tx.getName());
        TextView phone=findViewById(R.id.phone);
        phone.setText(tx.getNum());
        TextView remark=findViewById(R.id.deremark);
        remark.setText(tx.getRemark());
        Log.i(TAG,"7");
    }
    private void makePhoneCall(String num){
        Log.i(TAG,"8");
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + num));
        startActivity(callIntent);
    }
}